package com.gaowl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.gaowl.gulimall.product.entity.AttrEntity;
import com.gaowl.gulimall.product.service.AttrService;
import com.gaowl.gulimall.product.service.CategoryService;
import com.gaowl.gulimall.product.vo.AttrGroupRelationVo;
import com.gaowl.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gaowl.gulimall.product.entity.AttrGroupEntity;
import com.gaowl.gulimall.product.service.AttrGroupService;
import com.gaowl.common.utils.PageUtils;
import com.gaowl.common.utils.R;



/**
 * 属性分组
 *
 * @author gaowl
 * @email gaowl@stu.jiangnan.edu.cn
 * @date 2021-03-11 13:20:43
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    /**
     * 获取指定属性分组关联的所有属性
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> entityList = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", entityList);
    }

    /**
     * 获取『指定属性分组』里面还没有关联的『本分类里面的其他基本属性』，方便添加新的关联
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R getNoAttrRelation(@PathVariable("attrGroupId") Long attrGroupId,
                               @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(attrGroupId, params);
        return R.ok().put("page", page);
    }


    /**
     * 获取分类下所有分组&关联属性  /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAllGroupInfoWithAssociatedAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrGroupVoList = attrGroupService.findGroupInfoWithAssociatedAttrsByCatId(catelogId);
        return R.ok().put("data", attrGroupVoList);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.findCatelogPathById(attrGroup.getCatelogId());  // 寻找节点所在路径

		attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroupEntity){
		attrGroupService.updateById(attrGroupEntity);

        return R.ok();
    }

    /**
     * 删除属性
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 新增属性与分组的关联关系
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){
        attrService.addRelation(attrGroupRelationVos);
        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){
        attrService.deleteRelation(attrGroupRelationVos);
        return R.ok();
    }

}
